package design;

import design.forkjoin.RecureiveTask1;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;

public class ForkJoinTest2 {
    public static void main(String[] args) {
//        testGet();
        testJoin();
    }

    /**
     * 测试使用get方法获取运行结果，需要捕获InterrupuedException
     */
    public static void testGet() {
        RecureiveTask1 task1 = new RecureiveTask1();
        System.out.println("task1.hashCode() = " + task1.hashCode());
        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Integer> result = pool.submit(task1);
        try {
            Integer get = result.get();
            System.out.println(result.hashCode() + " " + get);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    /**
     * 测试使用join方法获取运行结果，需要捕获InterrupuedException
     */
    public static void testJoin() {
        RecureiveTask1 task1 = new RecureiveTask1();
        System.out.println("task1.hashCode() = " + task1.hashCode());
        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Integer> result = pool.submit(task1);
        Integer get = result.join();
        System.out.println(result.hashCode() + " " + get);
    }
}
